package com.huilian.cloud.module.system.service.menugroup;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.huilian.cloud.module.system.controller.admin.menugroup.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.menugroup.MenuGroupDO;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.util.object.BeanUtils;

import com.huilian.cloud.module.system.dal.mysql.menugroup.MenuGroupMapper;

import static com.huilian.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.huilian.cloud.module.system.enums.ErrorCodeConstants.*;

/**
 * 菜单分组类型关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MenuGroupServiceImpl implements MenuGroupService {

    @Resource
    private MenuGroupMapper menuGroupMapper;

    @Override
    public Integer createMenuGroup(MenuGroupSaveReqVO createReqVO) {
        // 插入
        MenuGroupDO menuGroup = BeanUtils.toBean(createReqVO, MenuGroupDO.class);
        menuGroupMapper.insert(menuGroup);
        // 返回
        return menuGroup.getGroupId();
    }

    @Override
    public void updateMenuGroup(MenuGroupSaveReqVO updateReqVO) {
        // 更新
        MenuGroupDO updateObj = BeanUtils.toBean(updateReqVO, MenuGroupDO.class);
        menuGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteMenuGroup(Integer id) {
        // 校验存在
        validateMenuGroupExists(id);
        // 删除
        menuGroupMapper.deleteById(id);
    }

    private void validateMenuGroupExists(Integer id) {
        if (menuGroupMapper.selectById(id) == null) {
            throw exception(MENU_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public MenuGroupDO getMenuGroup(Integer id) {
        return menuGroupMapper.selectById(id);
    }

    @Override
    public PageResult<MenuGroupDO> getMenuGroupPage(MenuGroupPageReqVO pageReqVO) {
        return menuGroupMapper.selectPage(pageReqVO);
    }

}